package ckgl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import xtgl.User;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class QtckController extends Controller {
	static int g_infNumber = 10;
	static int g_infNumber2 = 10;
	static String xqdh = null;
	static String wlbm = null;
	static String wlmc = null;
	static String ckdh = null;
	static String llr = null;

	public void index() {
		System.out.println("qqqqqqqqqq");
		g_infNumber = 10;
		g_infNumber2 = 10;
		xqdh = null;
		wlbm = null;
		wlmc = null;
		ckdh = null;
		llr = null;
		renderFreeMarker("/ckgl/qtck.html");
	}

	/* 待出库 */
	public void all() {
		if (getPara("xqdh") != null || getPara("wlbm") != null
				|| getPara("wlmc") != null) {
			xqdh = getPara("xqdh");
			wlbm = getPara("wlbm");
			wlmc = getPara("wlmc");
		}
		if (getParaToInt("infNumber") != null) {
			g_infNumber = getParaToInt("infNumber");
		}
		if (wlbm == null || xqdh == null || wlmc == null) {
			System.out.println("------------------");
			Page<?> qtck = Db.paginate(getParaToInt("pageNumber", 1),
					g_infNumber, "SELECT * ", "FROM qtck where zt='待出库'");

			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("qtck", qtck);

			List<Object> data = new ArrayList<Object>();
			data.add(map1);
			renderJson(data);
		} else {
			System.out.println("+++++++++" + xqdh);
			Page<?> qtck = Db.paginate(getParaToInt("pageNumber", 1),
					g_infNumber, "SELECT * ",
					"FROM qtck where zt='待出库' and wlbm like '%" + wlbm
							+ "%' and xqdh like '%" + xqdh
							+ "%' and wlmc like '%" + wlmc + "%'");
			setAttr("pN", qtck.getPageNumber());
			setAttr("tP", qtck.getTotalPage());
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("qtck", qtck);
			List<Object> data = new ArrayList<Object>();
			data.add(map1);
			renderJson(data);
		}

	}

	/* 已出库 */
	public void all2() {
		if (getPara("ckdh") != null || getPara("xqdh") != null
				|| getPara("wlbm") != null|| getPara("llr") != null ) {
			ckdh = getPara("ckdh");
			xqdh = getPara("xqdh");
			wlbm = getPara("wlbm");
			llr = getPara("llr");
		}
		if (getParaToInt("infNumber") != null) {
			g_infNumber2 = getParaToInt("infNumber");
		}
		if (ckdh == null || xqdh == null || wlbm == null|| llr == null) {
			Page<?> qtck = Db.paginate(getParaToInt("pageNumber", 1),
					g_infNumber2, "SELECT * ", "FROM qtck where zt='已出库'");

			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("qtck", qtck);

			List<Object> data = new ArrayList<Object>();
			data.add(map1);
			renderJson(data);
			System.out.println("===================");
		} else {
			
			Page<?> qtck = Db.paginate(getParaToInt("pageNumber", 1),
					g_infNumber2, "SELECT * ",
					"FROM qtck where zt='已出库' and ckdh like '%" + ckdh
							+ "%' and xqdh like '%" + xqdh
							+ "%' and wlbm like '%" + wlbm + "%'and llr like '%" + llr + "%'");
			setAttr("pN", qtck.getPageNumber());
			setAttr("tP", qtck.getTotalPage());
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("qtck", qtck);
			List<Object> data = new ArrayList<Object>();
			data.add(map1);
			renderJson(data);
		}

	}

	public void fanye() {
		g_infNumber = getParaToInt("infNumber");
		renderNull();
	}

	public void fanye2() {
		g_infNumber2 = getParaToInt("infNumber2");
		renderNull();
	}

	public void getInfNumber() {
		renderJson(g_infNumber);
	}

	public void getInfNumber2() {
		renderJson(g_infNumber2);
	}

	/* 出库 */
	public void ck() {

		User user = (User) getSession().getAttribute("user");
		String name = user.getStr("username"); /* 获取用户名 */
		String idlist = getPara("idlist");
		String ids[] = idlist.split(",");

		for (int j = 0; j < ids.length; j++) {
			int id = Integer.valueOf(ids[j]);

			List<Qtck> ccpp = Qtck.dao.find("select distinct ckdh FROM qtck ");
			int ttt = ccpp.size();// 先获得原有数据的条数
			System.out.println("++++++++++++++++++" + ttt);
			Date date = new Date();// 获取时间
			SimpleDateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd ");// 转换格式
			String dt = df1.format(date);
			String dt1 = "qtck";
			String dt3 = null;
			String dt4 = null;
			String dt5 = dt.substring(2, 4);
			String dt6 = dt.substring(5, 7);
			dt4 = dt5 + dt6 + "000";
			int ckdha = Integer.parseInt(dt4) + ttt + 1;
			String ckdhaa = String.valueOf(ckdha);
			dt3 = dt1 + ckdhaa;
			Qtck.dao.findById(id).set("ckdh", dt3).set("zt", "已出库")
					.set("cksj", df3.format(new Date())).set("ckr", name)
					.update();
			renderText("success");

		}
	}

	public void delete1() {
		Qtck.dao.deleteById(getParaToInt("id"));
		renderNull();
	}

	public void delete2() {
		Qtck.dao.deleteById(getParaToInt("id"));
		renderNull();
	}

	/* 修改 */
	public void updatea() {

		Qtck qtck = getModel(Qtck.class);
		qtck.update();
		renderText("success");
	}

	public void updateb() {

		Qtck qtck = getModel(Qtck.class);
		qtck.update();
		renderText("success");
	}

	/* 新增 */
	public void add() {
		Qtck qtck = getModel(Qtck.class);
		List<Qtck> qtck1 = Qtck.dao
				.find("select * from qtck where id in(select max(id) from qtck)");
		if (qtck1.size() == 0) {
			qtck.set("id", 1);
		} else {
			qtck.set("id", qtck1.get(0).getInt("id") + 1);
		}
		qtck.save();
		renderNull();
	}

}
